package com.shm.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.ProductCategory;
import com.ruoyi.common.core.domain.vo.AreaItemVo;
import com.ruoyi.common.core.domain.vo.ProductCategoryVo;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shm.mapper.ProductCategoryMapper;
import com.shm.service.IProductCategoryService;

/**
 * 商品分类Service业务层处理
 *
 * @author dam
 * @date 2023-07-11
 */
@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public ProductCategory selectProductCategoryById(Long id) {
        return productCategoryMapper.selectProductCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param productCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        return productCategoryMapper.selectProductCategoryList(productCategory);
    }

    /**
     * 新增商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertProductCategory(ProductCategory productCategory) {
        productCategory.setCreateTime(DateUtils.getNowDate());
        return productCategoryMapper.insertProductCategory(productCategory);
    }

    /**
     * 修改商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateProductCategory(ProductCategory productCategory) {
        productCategory.setUpdateTime(DateUtils.getNowDate());
        return productCategoryMapper.updateProductCategory(productCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByIds(Long[] ids) {
        return productCategoryMapper.deleteProductCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryById(Long id) {
        return productCategoryMapper.deleteProductCategoryById(id);
    }

    @Override
    public List<ProductCategoryVo> getProductCategoryTree(ProductCategory productCategory) {
        ////声明变量
        List<ProductCategoryVo> productCategoryVoList = new ArrayList<>();

        //查询出所有商品分类的信息
        List<ProductCategory> productCategoryEntityList = productCategoryMapper.selectProductCategoryList(null);
        //过滤出所有省
        List<ProductCategory> fatherList = productCategoryEntityList.stream().filter(item -> {
            if (item.getParentId() == 0) {
                return true;
            } else {
                return false;
            }
        }).collect(Collectors.toList());

        for (ProductCategory father : fatherList) {
//            ProductCategoryVo productCategoryVo = new ProductCategoryVo(father.getId(),father.getName());
            ProductCategoryVo productCategoryVo = new ProductCategoryVo();
            productCategoryVo.setId(father.getId());
            productCategoryVo.setName(father.getName());
            productCategoryVo.setIcon(father.getIcon());
            productCategoryVo.setSort(father.getSort());
            productCategoryVo.setDescription(father.getDescription());

            this.searchSon(productCategoryVo, productCategoryEntityList);
            productCategoryVoList.add(productCategoryVo);
        }

        //// 筛选出与搜索名字相关的数据
        if (productCategory.getName() != null) {
            List<ProductCategoryVo> newProductCategoryVoList = new ArrayList<>();
            for (ProductCategoryVo productCategoryVo : productCategoryVoList) {
                newProductCategoryVoList.addAll(filterRelatedData(productCategoryVo, productCategory.getName()));
            }
            productCategoryVoList = newProductCategoryVoList;
        }

        return productCategoryVoList;
    }

    /**
     * 筛选出和 keyword 相关的数据
     *
     * @param productCategoryVo
     * @param keyword
     * @return
     */
    private List<ProductCategoryVo> filterRelatedData(ProductCategoryVo productCategoryVo, String keyword) {
        List<ProductCategoryVo> productCategoryVoList = new ArrayList<>();
        if (productCategoryVo.getName().contains(keyword)) {
            productCategoryVoList.add(productCategoryVo);
        } else if (productCategoryVo.getChildren() != null && productCategoryVo.getChildren().size() > 0) {
            for (ProductCategoryVo child : productCategoryVo.getChildren()) {
                productCategoryVoList.addAll(filterRelatedData(child, keyword));
            }
        }
        return productCategoryVoList;
    }

    private void searchSon(ProductCategoryVo father, List<ProductCategory> productCategoryList) {
        List<ProductCategoryVo> sonList = productCategoryList.stream().filter(item -> {
            if (item.getParentId() == father.getId().intValue()) {
                return true;
            } else {
                return false;
            }
        }).map(item1 -> {
//            ProductCategoryVo son = new ProductCategoryVo(item1.getId(), item1.getName());
            ProductCategoryVo son = new ProductCategoryVo();
            son.setId(item1.getId());
            son.setName(item1.getName());
            son.setIcon(item1.getIcon());
            son.setSort(item1.getSort());
            son.setDescription(item1.getDescription());
            //继续给儿子寻找孙子
            this.searchSon(son, productCategoryList);
            return son;
        }).collect(Collectors.toList());
        father.setChildren(sonList);

    }
}
